#include "stdafx.h"

SceneView::SceneView(sim *_main) : main(_main){
	this->setBackgroundBrush(QBrush(QColor(54, 82, 120)));
	//this->setBackgroundBrush(QBrush(QColor(238, 241, 247)));
	allow_add_path = false;
}

SceneView::~SceneView() {

}

Node* SceneView::addNode(QPointF pos, QString name, bool visibleTitle){
	Node *node = new Node(this, name, pos);
	node_list.push_back(node);
	return node;
}

PipeLine* SceneView::addPipeLine(Node *sourceNode, Node *destNode, QString coordinates, QString name, bool visibleTitle){
	PipeLine *pipeline = new PipeLine(this, sourceNode, destNode, coordinates, name);
	sourceNode->addInletPipe(pipeline);
	destNode->addOutletPipe(pipeline);
	pipeline_list.push_back(pipeline);
	pipeline->title->setVisible(false);///
	return pipeline;
}

void SceneView::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent){
	if(allow_add_path){
		Node *node = checkAcceptedNode(mouseEvent->scenePos());
		if(node != NULL){
			PipeLine *pipeline = addPipeLine(virtual_node, node);
			log("Pipe: '" + pipeline->title->title() + "' has been added");
		}
	}
	allow_add_path = false;
	update();
	QGraphicsScene::mousePressEvent(mouseEvent);
}

void SceneView::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent){
	update();
	QGraphicsScene::mouseMoveEvent(mouseEvent);
}

void SceneView::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent){
	update();
	QGraphicsScene::mouseReleaseEvent(mouseEvent);
}

void SceneView::keyPressEvent(QKeyEvent *event){
	switch (event->key()) {
	case Qt::Key_Alt :
		beginDrawPipeline();
		break;
	case Qt::Key_C :
		if(event->modifiers() & Qt::ControlModifier) CopyItem();
		break;
	case Qt::Key_V :
		if(event->modifiers() & Qt::ControlModifier) PasteItem();
		break;
	case Qt::Key_X :
		if(event->modifiers() & Qt::ControlModifier) CutItem();
		break;
	case Qt::Key_Delete :
		DeleteItem();
		break;
	}
}

void SceneView::keyReleaseEvent(QKeyEvent *event){
	switch (event->key()) {
	case Qt::Key_Alt :
		endDrawPipeline();
		break;
	}
}

void SceneView::dragEnterEvent(QGraphicsSceneDragDropEvent *event){
	switch (virtual_item_type){
	case 0:
		virtual_node = new Node(this);
		addItem(virtual_node);
		break;
	}

}

void SceneView::dragMoveEvent(QGraphicsSceneDragDropEvent *event){
	switch (virtual_item_type){
	case 0:
		virtual_node->setPos(event->scenePos());
		virtual_node->update();
		break;
	case 1:
		checkAcceptedNode(event->scenePos());
		break;
	}
}

void SceneView::dropEvent(QGraphicsSceneDragDropEvent *event){
	switch (virtual_item_type){
	case 0:
		removeItem(virtual_node);
		log("Node: '" + addNode(event->scenePos())->title->title() + "' has been added");
		break;
	case 1:
		foreach(Node *node, node_list){
			if(node->isAccepted()){
				virtual_node = node;
				allow_add_path = true;
				break;
			}
		}
		break;
	}
}

void SceneView::dragLeaveEvent(QGraphicsSceneDragDropEvent *event){
	switch (virtual_item_type){
	case 0:
		removeItem(virtual_node);
		break;
	}
}


void SceneView::beginDrawPipeline(){

}

void SceneView::endDrawPipeline(){

}

void SceneView::CopyItem(){

}

void SceneView::PasteItem(){
	
}

void SceneView::CutItem(){
	
}

void SceneView::DeleteItem(){
	foreach(Node *node, node_list)
		if (node->isSelected()){
			log("Node: '" + node->title->title() + "' has been removed");
			delete node;
		}
	foreach(PipeLine *pipe, pipeline_list){
		pipe->setSelected(true);
		if (pipe->isSelected()){
			log("Path: '" + pipe->title->title() + "' has been removed");
			delete pipe;
		}
	}
}


Node* SceneView::checkAcceptedNode(const QPointF &pos){
	Node *accepted_node;
	bool accepted_node_exist = false;
	foreach (Node *node, node_list){
		QPointF distance = node->pos() - pos;
		node->setAccepted((abs(distance.x()) < 25 && abs(distance.y()) < 25));
		node->update();	
		if(node->isAccepted()) {
			accepted_node = node; 
			accepted_node_exist = true;
		}
	}
	if (accepted_node_exist) return accepted_node;
	return NULL;
}











